<?php

global $CFG;

require_once($CFG->libdir. '/simpletestlib/unit_tester.php');
require_once($CFG->libdir. '/simpletestlib/mock_objects.php');
require_once($CFG->libdir. '/simpletestlib.php');
require_once($CFG->libdir. '/ddllib.php');
require_once(dirname(__FILE__).'/../module_base.php');

class module_basetest extends module_base {
    public $editformfile;
    
    protected function get_edit_form_filename() {
        return $this->editformfile;
    }
    
    public function get_database_version() {
        return "2005112901";
    }
}

class module_base_test extends UnitTestCase {
    
    private $module;
    
    private $beforeprefix;
    
    const TESTPREFIX='unittest_';
    const TESTPREFIXORA='t_';
    
    private function getTestPrefix() {
        global $CFG;
        if (isset($CFG->dbfamily) && ($CFG->dbfamily == 'oracle')) {
            return self::TESTPREFIXORA;
        }
        return self::TESTPREFIX;
    }

    function setUp() {
        global $CFG, $db;
        $this->beforeprefix = $CFG->prefix;
        $CFG->prefix = $this->getTestPrefix();

        $this->module=new module_basetest();
                
        $this->delete_tables();

        make_test_tables_like_real_one(array(
            'course', 'modules', 'course_modules'),
            $this->beforeprefix, $this->getTestPrefix(), $db);

        // Add resourcepage course
        load_test_data($this->getTestPrefix().'course',array(
            array('id', 'shortname'),
            array(2,    'TESTBLEE')
            ));

        // Add modules
        load_test_data($this->getTestPrefix().'modules',array(
            array('id', 'name'),
            array(4,    'basetest')
            ));

        // Add resourcepage course_module
        load_test_data($this->getTestPrefix().'course_modules',array(
            array('id', 'course', 'module', 'instance'),
            array(3,    2,        4,        1)
            ));
    }
    
    function tearDown() {
        $this->delete_tables();
        // Put prefix back
        global $CFG;
        $CFG->prefix=$this->beforeprefix;
    }
    
    function delete_tables() {
        global $db;
        wipe_tables($this->getTestPrefix(), $db);
        wipe_sequences($this->getTestPrefix(), $db);
    }
    
    function test_install() {
        global $CFG;        

        $mod = 'resourcepage';
        $fullmod = $CFG->dirroot .'/mod/'. $mod;
        $status = false;
        if (file_exists($fullmod . '/db/install.xml')) {
            ob_start();
            $status = install_from_xmldb_file($fullmod . '/db/install.xml');
            ob_end_clean();
        }
        $this->assertTrue($status, 'Resourcepage module installation');
    }

    /*
     * TODO (sam), Use prefix and add fake database tables (course and cousremodules) 
     * so that the print_visible_setting print_groupmode_setting function calls don't
     * depend on a real course
     */
    function atest_editform() {
        $form = new stdClass;
        $form->course=1; // This must be a course that actually exists
        $form->sesskey=''; 
        $form->coursemodule=''; 
        $form->section=1; 
        $form->module=''; 
        $form->modulename=''; 
        $form->instance=''; 
        $form->mode=''; 
        
        $course = new stdClass;
        $course->id=1;
        $course->students='Users';
        $course->groupmode=0;
        $course->groupmodeforce=1;
        
        $this->innertest_editform('test',$form,$course);
    }
    
    function innertest_editform($name,$form,$course) {        
        // Get filenames
        $this->module->editformfile=dirname(__FILE__).'/editform.'.$name.'.xml';
        $resultpath=dirname(__FILE__).'/editform.'.$name.'.results.html';
        
        // Do test and capture its output
        ob_start();
        $this->module->show_edit_form($form,$course);
        $result=ob_get_contents();
        ob_end_clean();
        
        // Write to file (TEMP! Uncomment this to update with a new 'correct' version)
        file_put_contents(dirname(__FILE__).'/../../../../moodledata/editform.'.$name.'.results.html',$result);
        
        $compare=file_get_contents($resultpath);
        global $CFG;
        $compare=preg_replace('/http:\/\/sm449\.vledev\.open\.ac\.uk\/moodle/',$CFG->wwwroot,$compare);
        $this->assertExpectation(new IgnoreWhitespaceExpectation($compare),$result);
    }
    
    /*
     * TODO Use prefix and add and populate fake database tables (course and course_modules) 
     */
    function test_obtain_view_info1() {
        $cmtest = new stdClass;
        $cmtest->course=2;
        $cmtest->instance=1;
        $itemtest='TESTBLAH';
        $coursetest='TESTBLEE';
        
        // for following unit tests, module/class is actually 'basetest'!
        $sql = "
CREATE TABLE ".$this->getTestPrefix()."basetest
(
 id INTEGER NOT NULL ,
 course INTEGER DEFAULT 0 NOT NULL,
 name VARCHAR(255) DEFAULT ' ' NOT NULL,
CONSTRAINT ".$this->getTestPrefix()."basetest_pk PRIMARY KEY (id)
)
        ";
        if(!execute_sql($sql, false)) {
            throw new Exception("Error creating temp resourcepage/basetest table");
        } 

      // Add resourcepage/basetest
        $sql = "
INSERT INTO ".$this->getTestPrefix()."basetest 
 (id, course, name) VALUES
 (1, 2, 'TESTBLAH')
        ";
        if(!execute_sql($sql, false)) {
            throw new Exception("Error inserting into temp resourcepage/basetest table");
        } 

        $cm = null;
        $course = null;
        $item = null;
        $this->module->obtain_view_info(3,0,$cm,$course,$item);
        
        $this->assertEqual($cm->course,$cmtest->course);
        $this->assertEqual($cm->instance,$cmtest->instance);
        $this->assertEqual($course->shortname,$coursetest);
        $this->assertEqual($item->name,$itemtest);
    }
        
    /*
     * TODO Use prefix and add and populate fake database tables (course and course_modules) 
     */
    function test_obtain_view_info2() {
        $itemtest = new stdClass;
        $itemtest->course=2;
        $itemtest->id=1;
        $coursetest = new stdClass;
        $coursetest->id=2;
        $cmtest='brooogle';
        
        // for following unit tests, module/class is actually 'basetest'!
        $sql = "
CREATE TABLE ".$this->getTestPrefix()."basetest
(
 id INTEGER NOT NULL ,
 course INTEGER DEFAULT 0 NOT NULL,
 name VARCHAR(255) DEFAULT ' ' NOT NULL,
CONSTRAINT ".$this->getTestPrefix()."basetest_pk PRIMARY KEY (id)
)
        ";
        if(!execute_sql($sql, false)) {
            throw new Exception("Error creating temp resourcepage/basetest table");
        } 

      // Add resourcepage/basetest
        $sql = "
INSERT INTO ".$this->getTestPrefix()."basetest 
 (id, course, name) VALUES
 (1, 2, 'brooogle')
        ";
        if(!execute_sql($sql, false)) {
            throw new Exception("Error inserting into temp resourcepage/basetest table");
        } 

        $cm = null;
        $course = null;
        $item = null;
        $this->module->obtain_view_info(0,1,$cm,$course,$item);

        $this->assertEqual($cm->name,$cmtest);
        $this->assertEqual($course->id,$coursetest->id);
        $this->assertEqual($item->course,$itemtest->course);
        $this->assertEqual($item->id,$itemtest->id);
    }
    
// ou-specific begins
/*
    public function test_get_table_values() {
        $content2=new stdClass();
        $content2->id=666;
        $content2->version=42; // Put this/name in wrong order (not that it matters but)
        $content2->name=7; // Varchar
        $content2->cron=0;
        // leave out lastcron
        $content2->search=11; // Varchar column
        $content2->visible=''; // Int column
        $values=get_sql_table_values('mdl_modules',$content2);
        $this->assertEqual($values,
            array(
                'id' => '666',
                'name' => "'7'",
                'version'=>'42',
                'cron'=>'0',
                'search'=>"'11'",
                'visible'=>'0'
            ));               
    }
    
*/
// ou-specific ends
}
?>
